#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "MSTK.h"

#ifdef __cplusplus
extern "C" {
#endif

extern MPI_Datatype PVertex_MPI,PFace_MPI;

int MESH_Surf_SendMesh(Mesh_ptr mesh, int rank, MPI_Comm comm) {
  int i, j, k, idx, max_nfv;
  int nv, nf, nfv;
  MVertex_ptr mv;
  MFace_ptr mf;
  /* basic mesh information */
  nv = MESH_Num_Vertices(mesh);
  nf = MESH_Num_Faces(mesh);
  printf("PVertex_MPI datatype established\n");
  
  /* collect data */
  PVertex *list_vertex = (PVertex *)MSTK_malloc(nv*sizeof(PVertex));
  for(i = 0; i < nv; i++) {
    mv = MESH_Vertex(mesh,i);
    MV_to_PV(mv,&list_vertex[i]);
  }
  PFace *list_face = (PFace *)MSTK_malloc(nf*sizeof(PFace));
  for(i = 0; i < nf; i++) {
    mf = MESH_Face(mesh,i);
    MF_to_PF(mf,&list_face[i]);
  }
  /* send vertices */
  fprintf(stdout,"MPI_Send begins %d\n",rank);
  MPI_Send(list_vertex,nv,PVertex_MPI,rank,rank,comm);
  /* send faces */
  MPI_Send(list_face,nf,PFace_MPI,rank,100+rank,comm);
  fprintf(stdout,"MPI_Send finished %d\n",rank);
  
  MSTK_free(list_vertex);
  MSTK_free(list_face);
  return 1;
}
  
#ifdef __cplusplus
}
#endif

